package edu.hawaii.senin.iclouds.view;

import javax.swing.table.AbstractTableModel;

import edu.hawaii.senin.iclouds.basics.Reaction;
import edu.hawaii.senin.iclouds.simple.CloudReactions;

/**
 * Implements table container for the reactions collection.
 * 
 * @author Pavel Senin.
 * 
 */
public class ReactionTableModel
    extends AbstractTableModel {

  /**
   * Eclipse generated serial.
   */
  private static final long serialVersionUID = -4357342621930285712L;

  private static final String[] ColumnNames = { "Reaction", "Type", "E Barrier", "ChiValue",
      "Reactants", "Products" };

  private Object[][] rowData;

  /**
   * Generic constructor.
   * 
   */
  public ReactionTableModel() {
    super();
    this.rowData = new Object[1][6];
  }

  /**
   * Reports column count.
   * 
   * @return column count.
   */
  public int getColumnCount() {
    return ReactionTableModel.ColumnNames.length;
  }

  /**
   * Reports column name.
   * 
   * @param col column specified.
   * @return column name.
   */
  @Override
  public String getColumnName(int col) {
    return ReactionTableModel.ColumnNames[col];
  }

  /**
   * Reports table value.
   * 
   * @param rowIdx row specified.
   * @param colIdx column specified.
   * @return table value.
   */
  public Object getValueAt(int rowIdx, int colIdx) {
    return rowData[rowIdx][colIdx];
  }

  /**
   * Reports row count.
   * 
   * @return row count.
   */
  public int getRowCount() {
    return rowData.length;
  }

  /**
   * Reports whether or not cspecified cell is editable.
   * 
   * @param rowIdx row specified.
   * @param colIdx column specified.
   * @return true if editable.
   */
  @Override
  public boolean isCellEditable(int rowIdx, int colIdx) {
    return false;
  }

  /**
   * Sets the cell value.
   * 
   * @param value value to set.
   * @param rowIdx row specified.
   * @param colIdx column specified.
   */
  @Override
  public void setValueAt(Object value, int rowIdx, int colIdx) {
    rowData[rowIdx][colIdx] = value;
    fireTableCellUpdated(rowIdx, colIdx);
  }

  /**
   * Sets whole table with data provided.
   * 
   * @param store reactions collection.
   */
  public void setReactionData(CloudReactions store) {
    Integer rows = store.size();
    Integer columns = ColumnNames.length;
    rowData = new Object[rows][columns];
    Integer rowIdx = 0;
    for (Reaction r : store) {
      Object[] arr = r.toArray();
      for (int i = 0; i < arr.length; i++) {
        rowData[rowIdx][i] = arr[i];
      }
      rowIdx++;
    }
    fireTableDataChanged();
  }

}
